// Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

using DA = PfRep.DataAccess;
using PfRep.DataEntities;
using System.Data.SqlClient;

namespace PfRep.Library
{
    public static class Statistics
    {
        /// <summary>
        /// Gets the list of folder tree indexes
        /// </summary>
        /// <returns></returns>
        public static ArrayList GetFolderTrees()
        {
            using (SqlConnection sqlConnection = DA.SQL.CreateConnection())
            {
                return DA.SQL.GetFolderTrees(sqlConnection);
            }
        }

        /// <summary>
        /// Gets the list of content statistics
        /// </summary>
        /// <param name="folderTreeId"></param>
        /// <returns></returns>
        public static ArrayList GetContentStatistics(int folderTreeId)
        {
            using (SqlConnection sqlConnection = DA.SQL.CreateConnection())
            {
                return DA.SQL.GetContentStatistics(folderTreeId, sqlConnection);
            }
        }

        /// <summary>
        /// Gets the list of action plans
        /// </summary>
        /// <param name="folderTreeId"></param>
        /// <returns></returns>
        public static ArrayList GetActionPlans()
        {
            using (SqlConnection sqlConnection = DA.SQL.CreateConnection())
            {
                return DA.SQL.GetActionPlans(sqlConnection);
            }
        }

        /// <summary>
        /// Retrieves the subfolders of a given folder
        /// </summary>
        /// <param name="folderId">Parent Folder ID</param>
        /// <param name="folderTreeId">Folder Tree ID</param>
        /// <param name="contentStatId">Content statistics ID</param>
        /// <returns></returns>
        public static List<Folder> GetSubfolders(
            int folderId,
            int folderTreeId,
            int contentStatId)
        {
            using (SqlConnection sqlConnection = DA.SQL.CreateConnection())
            {
                return DA.SQL.GetSubfolders(
                    folderId, folderTreeId, contentStatId, sqlConnection);
            }
        }

        /// <summary>
        /// Gets all folders for a given tree Id
        /// </summary>
        /// <param name="folderTreeId"></param>
        /// <returns></returns>
        public static List<Folder> GetAllFolders(
            int folderTreeId)
        {
            using (SqlConnection sqlConnection = DA.SQL.CreateConnection())
            {
                return DA.SQL.GetAllFolders(folderTreeId, sqlConnection);
            }
        }

        /// <summary>
        /// Retrieves users starting with the given text
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public static ArrayList GetUsers(string filter)
        {
            return DA.AD.GetAdUsers(filter);
        }

        /// <summary>
        /// Updates a folder
        /// </summary>
        /// <param name="folder"></param>
        public static void UpdateFolder(
            DataEntities.Folder folder)
        {
            using (SqlConnection sqlConnection = DA.SQL.CreateConnection())
            {
                DA.SQL.UpdateFolder(folder.ID, folder.OwnerName, 
                    folder.OwnerEmail, (int)folder.ActionPlan, folder.Note, sqlConnection);
            }
        }

    }
}
